<?php


namespace app\admin\controller\mall;


use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\model\MallCate;
use app\common\model\MallGroupsGoods;
use app\common\model\MallGroupsOrder;
use app\common\model\MallManager;
use think\App;
use think\Model;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;

/**
 * Class Group
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="拼团管理")
 */
class Group extends AdminController
{
    use Curd;
    protected $relationSearch = true;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new MallGroupsGoods();
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index($id='')
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();

            foreach($list as $k=>$v){
                $cat =(new MallCate())->where("id='".$v['cate_id']."'")->find();
                $list[$k]['catname'] =$cat['title'];
            }

            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        $this->assign('mid',$id);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="添加")
     */
    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            $post['content'] =htmlspecialchars_decode($post['content']);
            $post['endtime'] =strtotime($post['endtime']);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $cats =(new MallCate())->field('id,title')->where("type=1")->select()->toArray();
        $manager =(new MallManager())->field('id,title')->where("applystatus=1")->select()->toArray();
        $this->assign('manager',$manager);
        $this->assign('cats',$cats);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {
        $row = $this->model->find($id);
        empty($row) && $this->error('数据不存在');
        if ($this->request->isPost()) {
            $post = $this->request->post();
            $rule = [];
            $this->validate($post, $rule);
            $post['content'] =htmlspecialchars_decode($post['content']);
            $post['endtime'] =strtotime($post['endtime']);
            try {
                $save = $row->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $cats =(new MallCate())->field('id,title')->where("type=1")->select()->toArray();
        $manager =(new MallManager())->field('id,title')->where("applystatus=1")->select()->toArray();
        $this->assign('manager',$manager);
        $this->assign('cats',$cats);
        $this->assign('row', $row);
        return $this->fetch();
    }

    /**
     * 使用门店管理
     * @return void
     */
    public function mendian(){
        return $this->fetch();
    }

}